﻿/// <reference path="../classes.js" />
/// <reference path="qunit-1.11.0.js" />

// Methos for testing:
//  - initialize
//  - numberOfWorkers
//  - numberOfMachines
//  - addBuilding
//  - renderPartial
//  - renderFull

(function () {
    module("Initialization");
    //function (town, numberOfPotentialClients) 
    test("Initialization with no parameters", function () {
        var branch = new Branch();
        equal(branch.town, undefined, "Town is undefiend");
        equal(branch.numberOfPotentialClients, undefined, "Number of clients is undefiend");
        equal(branch.buildings.length, 0, "Number of building is 0");
    });
    test("Full initialization", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        equal(branch.town, town, "Town");
        equal(branch.numberOfPotentialClients, numberOfPotentialClients, "Number of clients");
        equal(branch.buildings.length, 0, "Number of building is 0");
    });

    module("numberOfWorkers");
    test("Zero workers", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        equal(branch.numberOfWorkers(), 0, "Numbers of workers is 0");
    });
    test("in ProductionBuilding, should be return 50", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        var building = new ProductionBuilding();
        for (var i = 0; i < 50; i++) {
            building.addWorker(new Worker("John", "Bon Jovi", 32, 12));
        }
        branch.addBuilding(building);
        equal(branch.numberOfWorkers(), 50, "Numbers of workers is 50");
    });
    test("in AdministrativeBuilding, should be return 20", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        var building = new AdministrativeBuilding();
        for (var i = 0; i < 20; i++) {
            building.addWorker(new Worker("John", "Bon Jovi", 32, 12));
        }
        branch.addBuilding(building);
        equal(branch.numberOfWorkers(), 20, "Numbers of workers is 20");
    });
    test("in complete building with workers, experts and manager", function () {
        var town = "New York",
           numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        var building = new ProductionBuilding();
        for (var i = 0; i < 50; i++) {
            building.addWorker(new Worker("John", "Bon Jovi", 32, 12));
        }
        for (var i = 0; i < 9; i++) {
            building.addWorker(new Expert("Clint", "Eastwood", 70, 48, "Physical education"));
        }
        building.addWorker(new Manager("Petar", "Petrov", 30, 8, "Economics"));
        branch.addBuilding(building);
        equal(branch.numberOfWorkers(), 60, "Numbers of workers is 60");
    });

    module("numberOfMachines");
    test("Zero machines", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        equal(branch.numberOfMachines(), 0, "Zero machines");
    });
    test("Three machines of every kind", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        var building = new ProductionBuilding();
        building.addMachine(new Machine(3, 4, 19));
        building.addMachine(new Server(10, 3, 5, 12, "Ubutu", "data processing"));
        building.addMachine(new Vehicle(10, 3, 5, 8, "yes", "Van"));
        branch.addBuilding(building);
        equal(branch.numberOfMachines(), 3, "Three machines are added");
    });
    test("Two machines of one kind", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        var building = new ProductionBuilding();
        building.addMachine(new Machine(3, 4, 19));
        building.addMachine(new Server(10, 3, 5, 12, "Ubutu", "data processing"));
        building.addMachine(new Server(5, 3, 5, 12, "Widnows", "data processing"));
        branch.addBuilding(building);
        equal(branch.numberOfMachines(), 3, "Three machines are added, two of them are servers");
    });

    module("addBuilding");
    test("no building is given, should throws excepion", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        throws(function () {
            branch.addBuilding();
        }, "Throws an exception");
    });
    test("Worker is given as parameter, should throws excepion", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        throws(function () {
            branch.addBuilding(new Worker());
        }, "Throws an exception");
    });
    test("Add a ProductionBuilding", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        branch.addBuilding(new ProductionBuilding());
        equal(branch.buildings.length, 1, "One Production building is added");
    });
    test("Add a AdministrativeBuilding", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        branch.addBuilding(new AdministrativeBuilding());
        equal(branch.buildings.length, 1, "One Administrative building is added");
    });
    test("Add building of every kind", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        branch.addBuilding(new Building());
        branch.addBuilding(new ProductionBuilding());
        branch.addBuilding(new AdministrativeBuilding());
        equal(branch.buildings.length, 3, "Building of every kind is added");
    });

    module("renderPartial");
    test("Render branch with no parameters", function () {
        var branch = new Branch();

        var fixture = document.getElementById("qunit-fixture");
        var container = branch.renderPartial();
        $(fixture).append(container);

        var generatedDivs = fixture.getElementsByTagName("div");
        equal(generatedDivs.length - 2, 0, "Nothing is generated");
    });
    test("Render branch with parameters and no buildings", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);

        var fixture = document.getElementById("qunit-fixture");
        var container = branch.renderPartial();
        $(fixture).append(container);

        var generatedDivs = fixture.getElementsByClassName("branch");
        equal(generatedDivs.length, 1, "branch div with id=\"branch\" is generated");
    });
    test("Render branch with one building", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        branch.addBuilding(new Building("Some", 350, 1000000, false));

        var fixture = document.getElementById("qunit-fixture");
        var container = branch.renderPartial();
        $(fixture).append(container);

        var generatedDivs = fixture.getElementsByClassName("branch");
        equal(generatedDivs.length, 1, "branch div with id=\"branch\" is generated");
        var buildings = fixture.getElementsByClassName("building");
        equal(buildings.length, 1, "Building is added to the branch");
    });
    test("Render branch with three buildings", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        branch.addBuilding(new Building("Some", 350, 1000000, false));
        branch.addBuilding(new ProductionBuilding("Production 2", 560, 300000, false));
        branch.addBuilding(new AdministrativeBuilding("Plovdiv admin", 2000, 3000000, false));

        var fixture = document.getElementById("qunit-fixture");
        var container = branch.renderPartial();
        $(fixture).append(container);

        var generatedDivs = fixture.getElementsByClassName("branch");
        var buildings = fixture.getElementsByClassName("building");
        var productBuilding = fixture.getElementsByClassName("production");
        var adminBuilding = fixture.getElementsByClassName("administrative");

        equal(generatedDivs.length, 1, "branch div with id=\"branch\" is generated");
        equal(buildings.length, 3, "Building is added to the branch");
        equal(productBuilding.length, 1, "Production building is added to the branch");
        equal(adminBuilding.length, 2, "Administrative building is added to the branch");
    });

    module("renderFull");
    test("Render branch with no parameters", function () {
        var branch = new Branch();

        var fixture = document.getElementById("qunit-fixture");
        var container = branch.renderFull();
        $(fixture).append(container);

        var generatedDivs = fixture.getElementsByTagName("div");
        equal(generatedDivs.length, 7);
        var generateClassContainer = fixture.getElementsByClassName("branch-full");
        equal(generateClassContainer.length, 1, "Class container with id=\"branch-full\" is generated");
    });
    test("Render branch with three buildings", function () {
        var town = "New York",
            numberOfPotentialClients = 1000;

        var branch = new Branch(town, numberOfPotentialClients);
        branch.addBuilding(new Building("Some", 350, 1000000, false));
        branch.addBuilding(new ProductionBuilding("Production 2", 560, 300000, false));
        branch.addBuilding(new AdministrativeBuilding("Plovdiv admin", 2000, 3000000, false));

        var fixture = document.getElementById("qunit-fixture");
        var container = branch.renderFull();
        $(fixture).append(container);

        var fixtureDivs = fixture.getElementsByTagName("div");
        var generatedDivs = fixture.getElementsByClassName("branch-full");

        equal(generatedDivs.length, 1, "branch div with id=\"branch\" is generated");
        equal(fixtureDivs.length-3, 4);
    });
}());
